package com.wl.dzt.sys.response;

import com.wl.dzt.sys.util.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;


/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 统一的响应对象
 */
@ApiModel("Result响应对象")
public class Result implements Serializable {

    @ApiModelProperty("状态码")
    private int code;
    @ApiModelProperty("是否成功")
    private boolean success;
    @ApiModelProperty("错误字段")
    private Set<String> fields = new HashSet<>();
    @ApiModelProperty("错误消息")
    private String msg;
    @ApiModelProperty("数据对象")
    private Object data;

    public Set<String> getFields() {
        return fields;
    }

    public void setFields(Set<String> fields) {
        this.fields = fields;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Result() {
    }

    private Result(boolean flag) {
        this.success = flag;
        this.code = flag ? 200 : 500;
        this.msg = ResponseMsg.getMsg(flag ? "200" : "500");
    }

    private Result(boolean flag, String msgCode) {
        this.success = flag;
        this.code = flag ? 200 : 500;
        this.msg = ResponseMsg.getMsg(msgCode);
    }

    private Result(boolean flag, Object data) {
        this.success = flag;
        this.code = flag ? 200 : 500;
        this.msg = ResponseMsg.getMsg(flag ? "200" : "500");
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Result(int code, boolean success, Set<String> fields, String msg, Object data) {
        this.code = code;
        this.success = success;
        this.fields = fields;
        this.msg = msg;
        this.data = data;
    }


    /**
     * 成功
     * @return
     */
    public static Result success() {
        return new Result(true);
    }

    /**
     * 成功,且添加状态码消息
     * @param msgCode
     * @return
     */
    public static Result success(String msgCode) {
        return new Result(true, msgCode);
    }

    /**
     * 成功,且添加数据对象
     * @param data
     * @return
     */
    public static Result success(Object data) {
        return new Result(true, data);
    }

    /**
     * 失败
     * @return
     */
    public static Result error() {
        return new Result(false);
    }


    /**
     * 失败,且状态码消息
     * @param msgCode
     * @return
     */
    public static Result error(String msgCode) {
        return new Result(false, msgCode);
    }

    /**
     * 添加对应状态码信息
     * @param msgCode
     * @return
     */
    public Result addMsg(String msgCode) {
        this.msg = ResponseMsg.getMsg(msgCode);
        return this;
    }

    /**
     * 添加数据对象
     * @param data
     * @return
     */
    public Result addData(Object data) {
        this.data = data;
        return this;
    }

    /**
     * 添加错误字段.
     * @param field
     * @return
     */
    public Result addField(String... field) {
        this.fields.addAll(Arrays.asList(field));
        return this;
    }


}
